#!/bin/bash
#设置任务异常退出
set -eo pipefail
dt='{{ execution_date | cst_ds }}'
name='中心集散进港建包汇总数据'
# 在退出时清理文件
trap "rm -f /tmp/${name}_${dt}.zip /tmp/${name}_${dt}.csv && echo 'removed tmp files'" EXIT
# 创建txt文件
touch /tmp/${name}_${dt}.txt
#将中文字段名输入文件顶部
#将查询结果追加到文件中
{
  echo -e "日期\t扫描网点编号\t扫描网点名称\t所属代理区code\t所属代理区名称\t扫描网点类型2转运中心3集散点\t进港总发件量\t应建包票数\t进港建包票数\t应建未建票数\t进港建包数\t一段码包票数\t一段码包数\t循环袋包内件票数\t循环袋包数\t错建包票数\t文件类应建包票数\t文件类已建包票数\t总建包票数">/tmp/${name}_${dt}.txt;
  spark-sql --driver-memory 8G --num-executors 10 --executor-memory 12g --executor-cores 4  --conf spark.executor.memoryOverhead=4g --conf spark.driver.maxResultSize=3g -S -e "
select nvl(date_time, ' ')               as date_time
     , nvl(scan_site_code, ' ')          as scan_site_code
     , nvl(input_site, ' ')              as input_site
     , nvl(site_agent_code, ' ')         as site_agent_code
     , nvl(site_agent_name, ' ')         as site_agent_name
     , nvl(network_type, ' ')            as network_type
     , nvl(way_net_total, ' ')           as way_net_total
     , nvl(need_way_num, ' ')            as need_way_num
     , nvl(in_pack_way_num, ' ')         as in_pack_way_num
     , nvl(need_no_way_num, ' ')         as need_no_way_num
     , nvl(pack_num, ' ')                as pack_num
     , nvl(first_pack_way_num, ' ')      as first_pack_way_num
     , nvl(first_pack_num, ' ')          as first_pack_num
     , nvl(loop_in_pack_way_num, ' ')    as loop_in_pack_way_num
     , nvl(loop_pack_num, ' ')           as loop_pack_num
     , nvl(error_pack_way_num, ' ')      as error_pack_way_num
     , nvl(need_pack_direc_way_num, ' ') as need_pack_direc_way_num
     , nvl(have_pack_direc_way_num, ' ') as have_pack_direc_way_num
     , nvl(hav_pack_way_num, ' ')        as hav_pack_way_num
from jms_dm.dm_tab_in_center_package_site_summary_dt
where dt = '{{ execution_date | cst_ds }}'
;
 " >>/tmp/${name}_${dt}.txt
}

# 替换分隔符，并输出csv
cat /tmp/${name}_${dt}.txt | tr -s '\t' ',' > /tmp/${name}_${dt}.csv
# 压缩并删除 csv 文件
zip -jrm /tmp/${name}_${dt}.zip /tmp/${name}_${dt}.csv
# 上传文件
/usr/local/python3/bin/python3  /usr/local/airflow/dags/utils/operators/upload_oss_operation.py ${name}/${name}_${dt}.zip /tmp/${name}_${dt}.zip
# zip 文件将在进程退出后被自动清理

#声明文件大小
data_size=$(ls -lh /tmp/${name}_${dt}.zip | awk '{print $5}')
#打印文件信息
Message="${name}_${dt}.zip 上传成功 大小: $data_size"
#配置钉钉告警
curl 'https://oapi.dingtalk.com/robot/send?access_token=816a8f35fd84432ffa6bff9d7714abc322628bb4a8ded86908550d2335aba613' \
   -H 'Content-Type: application/json' \
   -d "
  {\"msgtype\": \"text\",
    \"text\": {
        \"content\": \"监控内容:$Message\"
     }
  }" || true
rm -f  /tmp/${name}_${dt}.txt